package com.ff.miaosha.service;

import com.ff.miaosha.dao.OrderDao;
import com.ff.miaosha.domain.MiaoshaOrder;
import com.ff.miaosha.domain.MiaoshaUser;
import com.ff.miaosha.domain.OrderInfo;
import com.ff.miaosha.redis.MiaoshaKey;
import com.ff.miaosha.redis.RedisService;
import com.ff.miaosha.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class OrderService {

    @Autowired
    OrderDao orderDao;

    @Autowired
    RedisService redisService;

    public OrderInfo createOrder(MiaoshaUser user, GoodsVo goods) {
        OrderInfo orderInfo=new OrderInfo();
        orderInfo.setCreateDate(new Date());
        orderInfo.setDeliveryAddrId(0);
        orderInfo.setGoodsCount(1);
        orderInfo.setGoodsId(goods.getId());
        orderInfo.setGoodsName(goods.getGoodsName());
        orderInfo.setGoodsPrice(goods.getMiaoshaPrice());
        orderInfo.setOrderChannal(1);
        orderInfo.setStatus(0);
        orderInfo.setUserId(user.getId());
        //增加联合主键要先删除之前的主键，删除主键要更新字段参数，不能有自增
        long orderId=orderDao.insert(orderInfo);
        MiaoshaOrder miaoshaOrder=new MiaoshaOrder();
        miaoshaOrder.setGoodsId(goods.getId());
        miaoshaOrder.setOrderId(orderId);
        miaoshaOrder.setUserId(user.getId());
        orderDao.insertMiaoshaOrder(miaoshaOrder);
        return orderInfo;
    }

    public MiaoshaOrder getMiaoshaOrderByUserIdGoodsId(long id, long goodsId) {
        return orderDao.getMiaoshaOrderByUserIdGoodsId(id,goodsId);
    }

    public OrderInfo getOrderById(long orderId) {
       return orderDao.getOrderById(orderId);
    }
}
